local jiaoxia = fk.CreateSkill {

  name = "joy_jiaoxia",

  tags = {  },

}



jiaoxia:addEffect("viewas", {
  name = "joy_jiaoxia",
  anim_type = "offensive",
  pattern = "slash",
  prompt = "你可以将手牌当做【杀】使用，若造成伤害，结算时你可以使用原卡牌！",
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand
  end,
  view_as = function(self, player,cards)
    if #cards ~= 1 then return nil end
    local c = Fk:cloneCard("slash")
    c.skillName = jiaoxia.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_play = function(self, player)
    return player.phase == Player.Play
  end,
  enabled_at_response = function(self, player, response)
    return false
  end,
})

jiaoxia:addEffect("targetmod", {
  name = "#joy_jiaoxia_targetmod",
  bypass_times = function(self, player, skill, scope, card, to)
    return player:hasSkill(jiaoxia.name) and card and card.trueName == "slash" and to and
    not table.contains(player:getTableMark("joy_jiaoxia_target-phase"), to.id)
  end,
  bypass_distances =  function(self, player, skill, card, to)
    return player:hasSkill(jiaoxia.name) and card and card.trueName == "slash" and to and
    not table.contains(player:getTableMark("joy_jiaoxia_target-phase"), to.id)
  end,
})

jiaoxia:addEffect(fk.CardUseFinished, {
  name = "#joy_jiaoxia_delay",
  anim_type = "offensive",
  events = {fk.CardUseFinished, fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(jiaoxia.name) then return false end
    if event == fk.TargetSpecified then
      return data.card.trueName == "slash" and player.phase == player.Play and
      not table.contains(player:getTableMark("joy_jiaoxia_target-phase"), data.to)
    elseif event == fk.CardUseFinished then
      if table.contains(data.card.skillNames, "joy_jiaoxia") and data.damageDealt then
        local card = Fk:getCardById(data.card:getEffectiveId())
        return player:canUse(card) and not player:prohibitUse(card) and player.room:getCardArea(card) == Card.Processing
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
      return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      room:addTableMark(player, "joy_jiaoxia_target-phase", data.to)
    elseif event == fk.CardUseFinished then
      local ids = Card:getIdList(data.card)
      room:askForUseRealCard( player, ids, ".", jiaoxia.name, "#joy_jiaoxia-use:::"..Fk:getCardById(ids[1]):toLogString(),
      { expand_pile = ids, bypass_times = false, extraUse = false })
    end
  end,
})
jiaoxia:addEffect(fk.TargetSpecified, {
  name = "#joy_jiaoxia_delay",
  anim_type = "offensive",
  events = {fk.CardUseFinished, fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(jiaoxia.name) then return false end
    if event == fk.TargetSpecified then
      return data.card.trueName == "slash" and player.phase == player.Play and
      not table.contains(player:getTableMark("joy_jiaoxia_target-phase"), data.to)
    elseif event == fk.CardUseFinished then
      if table.contains(data.card.skillNames, "joy_jiaoxia") and data.damageDealt then
        local card = Fk:getCardById(data.card:getEffectiveId())
        return player:canUse(card) and not player:prohibitUse(card) and player.room:getCardArea(card) == Card.Processing
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
      return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      room:addTableMark(player, "joy_jiaoxia_target-phase", data.to)
    elseif event == fk.CardUseFinished then
      local ids = Card:getIdList(data.card)
      room:askForUseRealCard( player, ids, ".", jiaoxia.name, "#joy_jiaoxia-use:::"..Fk:getCardById(ids[1]):toLogString(),
      { expand_pile = ids, bypass_times = false, extraUse = false })
    end
  end,
})
jiaoxia:addEffect(fk.PreCardUse, {
  refresh_events = {fk.PreCardUse},
  can_refresh = function(self, event, target, player, data)
    if player == target and data.card.trueName == "slash" and player.phase == Player.Play and player:hasSkill(jiaoxia.name) then
      local mark = player:getTableMark("joy_jiaoxia_target-phase")
      return table.find(data:getAllTargets(), function (pid)
        return not table.contains(mark, pid)
      end)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    data.extraUse = true
  end,
})

return jiaoxia